package com.bt.mes.common.util;


import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;

import javax.annotation.Resource;

import org.apache.commons.betwixt.io.BeanWriter;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.CompactWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;

@Component
public class MqOpe {

	@Resource
	private TableConfigUtil tableConfigUtil;

	private Logger logger = Logger.getLogger(MqOpe.class);

	public Object inStr2Obj(String strInMsg, Object inObj) {
		Object objIn = new Object();
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("transaction", inObj.getClass());
		xstream.processAnnotations(inObj.getClass());
		objIn = xstream.fromXML(strInMsg);
		return objIn;
	}

	public String obj2Str(Object inObj) {
		String relsultXml = null;
		OutputStream output = new ByteArrayOutputStream();
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("transaction", inObj.getClass());
		xstream.autodetectAnnotations(true);
		xstream.setMode(XStream.NO_REFERENCES);//不使用no_references

		xstream.marshal(inObj, new CompactWriter(new OutputStreamWriter(output)));
		relsultXml = output.toString().replace("__", "_");// .replace("<id><","<").replace("></id>",">");

		// writeQuery(relsultXml, inObj.getClass().getSimpleName());

		return relsultXml;
	}

	public String obj2Str2(Object inObj) throws Exception {
		String relsultXml = null;
		OutputStream output = new ByteArrayOutputStream();
		XStream xstream = new XStream(new DomDriver());
		try {
			xstream.alias("transaction", inObj.getClass());
			xstream.autodetectAnnotations(true);
			xstream.setMode(XStream.NO_REFERENCES);//不使用no_references

			xstream.marshal(inObj, new CompactWriter(new OutputStreamWriter(output)));
			relsultXml = output.toString().replace("__", "_");// .replace("<id><","<").replace("></id>",">");
		} catch (Exception e) {
			logger.error(Util.stackTraceToString(e));
			throw e;
		}

		// writeQuery(relsultXml, inObj.getClass().getSimpleName());

		return relsultXml;
	}

	public String hisQueryReport(Object inObj) {
		String strTableNameUp = inObj.getClass().getSimpleName().toUpperCase();
		boolean sendHisFlg = tableConfigUtil.checkSendHis(strTableNameUp);
		if (!sendHisFlg) {
			return null;
		}
		StringWriter sw = new StringWriter();
		sw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><transaction><trans_cnt>1</trans_cnt>");
		String replacedOutXml = null;
		BeanWriter beanWriter = new BeanWriter(sw);
		beanWriter.setEndOfLine("");
		beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
		beanWriter.getBindingConfiguration().setMapIDs(false);
		try {
			beanWriter.write("table", inObj);

			sw.write("</transaction>");
			/**
			 * TODO:去掉 id节点,目前使用Trim方法,是否还有更优解决方案
			 */
			replacedOutXml = StringUtils.replaceEach(sw.toString(), new String[] { "<id>", "</id>" }, new String[] { "", "" });

		} catch (Exception e) {
			logger.error(e.toString());
		} finally {
			try {
				sw.close();
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return replacedOutXml;
	}

}
